package com.cloud.tsp.controller;

import com.cloud.api.constants.StrVo;
import com.cloud.api.domain.command.CommandBatch;
import com.cloud.api.domain.model.ModelCommand;
import com.cloud.api.domain.tsp.*;
import com.cloud.api.validgroup.ValidateGroupInsert;
import com.cloud.api.validgroup.ValidateGroupUpdate;
import com.cloud.core.exception.MyException;
import com.cloud.core.util.R;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.tsp.service.ICommandService;
import com.cloud.tsp.service.IVehicleMessageDailyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/command")
public class CommandController {

    @Resource
    private ICommandService commandService;
    @Resource
    private IVehicleMessageDailyService vehicleMessageDailyService;

    @Tag(name = "v1.0")
    @Operation(summary = "通过vin的id获取指令列表（管理）")
    @PostMapping("/getCommand")
    @Log(title = "指令管理-通过vin获取指令列表", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<Command> selectCommand(@RequestBody @Validated StrVo para){
        List<Command> commands = commandService.selectCommand(para.getId());
        if (commands.isEmpty()){
            throw new MyException("wds.exception.notCommand");
        }
        return R.ok(commands);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "通过车型获取指令列表（管理）")
    @PostMapping("/getCommandById")
    @Log(title = "指令管理-通过车型获取指令列表", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<ModelCommand> selectCommandById(@RequestBody @Valid StrVo para){
        return R.ok(commandService.selectGroupCommandById(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "添加指令（管理）")
    @PostMapping("/insertCommand")
    @Log(title = "指令管理-添加指令", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R insertCommand(@Validated(ValidateGroupInsert.class) @RequestBody @Valid Command para){
        commandService.insertCommand(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "批量添加指令（管理）")
    @PostMapping("/insertCommandBatch")
    @Log(title = "指令管理-批量添加指令", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R insertCommandBatch(@Validated @RequestBody CommandBatch para){
        commandService.insertCommandBatch(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "修改指令（管理）")
    @PostMapping("/updateCommand")
    @Log(title = "指令管理-修改指令", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R updateCommand(@RequestBody @Valid CommandRequest para){
        commandService.updateCommand(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "修改指令状态（管理）")
    @PostMapping("/updateCommandStatus")
    @Log(title = "指令管理-修改指令状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R updateCommandStatus(@Validated(ValidateGroupUpdate.class) @RequestBody @Valid Command para){
        commandService.updateCommandStatus(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "删除指令（管理）")
    @PostMapping("/deleteCommand")
    @Log(title = "指令管理-删除指令", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deleteCommand(@RequestParam String id,@RequestParam String groupName){
        commandService.deleteCommand(id,groupName);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "删除控车项（管理）")
    @PostMapping("/deleteControlGroup")
    @Log(title = "指令管理-删除控车项", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deleteControlGroup(@RequestBody @Validated DelGroupRequest para){
        commandService.deleteControlGroup(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "验证码下发指令（管理）")
    @PostMapping("/issueCommand")
    @Log(title = "指令管理-验证码下发指令", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R issueCommand(@RequestBody @Valid IssueCommandByCode para){
        commandService.issueCommand(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "获取车辆当前自定义状态")
    @PostMapping("/getCustomStatus")
    @Log(title = "获取车辆当前自定义状态", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleCustomStatus> getCustomStatus(@RequestBody @Validated StrVo para){
        return R.ok(vehicleMessageDailyService.getCustomStatus(para));
    }

}